## TV2 Sumo have an API but it isn't for public use
module Shin
  module Play
    class Tv2sumo

      def new
        self
      end

      # All tv series
      def series
        # Response
        response = Base.get('https://sumo.tv2.no/rest/categories/90316/shows?sort=alphabetical&size=999')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['shows'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # All movies
      def movies
        # Response
        response = Base.get('https://sumo.tv2.no/rest/categories/90987/movies?sort=alphabetical&size=999')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['assets'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # All children shows
      def junior
        # Response
        response = Base.get('https://sumo.tv2.no/rest/categories/91101/shows?sort=alphabetical&size=999')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['shows'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Errors
      class NotValid         < StandardError; end
      class MissingArgument  < StandardError; end
      class HTTPError        < StandardError; end
    end
  end
end
